package com.gitee.wsl.data.pool.sample

abstract class SimpleCreatePool<T>(maxPoolSize: Int) : SimplePool<T>(maxPoolSize) {

    override fun acquire(): T {
        var ret = super.acquire()
        if(ret == null){
            ret = produceInstance()
            release(ret)
        }
        return ret!!
    }

    /**
     * Creates a new instance of [T]
     */
    protected abstract fun produceInstance(): T

    /**
     * Dispose [instance] and release its resources
     */
    protected open fun disposeInstance(instance: T) {}

    /**
     * Clear [instance]'s state before reuse: reset pointers, counters and so on
     */
    protected open fun clearInstance(instance: T): T = instance

    /**
     * Validate [instance] of [T]. Could verify that the object has been borrowed from this pool
     */
    protected open fun validateInstance(instance: T) {}
}